﻿using System;
using System.Runtime.CompilerServices;

namespace UniRx.Operators
{
	// Token: 0x0200025A RID: 602
	internal class CatchObservable<T, TException> : OperatorObservableBase<T> where TException : Exception
	{
		// Token: 0x06000DC8 RID: 3528 RVA: 0x00041540 File Offset: 0x0003F940
		public CatchObservable(IObservable<T> source, Func<TException, IObservable<T>> errorHandler) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.errorHandler = errorHandler;
		}

		// Token: 0x06000DC9 RID: 3529 RVA: 0x0004155C File Offset: 0x0003F95C
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new CatchObservable<T, TException>.Catch(this, observer, cancel).Run();
		}

		// Token: 0x040007CF RID: 1999
		private readonly IObservable<T> source;

		// Token: 0x040007D0 RID: 2000
		private readonly Func<TException, IObservable<T>> errorHandler;

		// Token: 0x0200025B RID: 603
		private class Catch : OperatorObserverBase<T, T>
		{
			// Token: 0x06000DCA RID: 3530 RVA: 0x0004156B File Offset: 0x0003F96B
			public Catch(CatchObservable<T, TException> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000DCB RID: 3531 RVA: 0x0004157C File Offset: 0x0003F97C
			public IDisposable Run()
			{
				this.serialDisposable = new SerialDisposable();
				this.serialDisposable.Disposable = this.parent.source.Subscribe(this);
				return this.serialDisposable;
			}

			// Token: 0x06000DCC RID: 3532 RVA: 0x000415AB File Offset: 0x0003F9AB
			public override void OnNext(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000DCD RID: 3533 RVA: 0x000415BC File Offset: 0x0003F9BC
			public override void OnError(Exception error)
			{
				//TException ex = error as TException;
				//if (ex != null)
				//{
				//	IObservable<T> observable;
				//	try
				//	{
				//		Delegate errorHandler = this.parent.errorHandler;
				//		if (CatchObservable<T, TException>.Catch.<>f__mg$cache0 == null)
				//		{
				//			CatchObservable<T, TException>.Catch.<>f__mg$cache0 = new Func<TException, IObservable<T>>(Stubs.CatchIgnore<T>);
				//		}
				//		if (errorHandler == CatchObservable<T, TException>.Catch.<>f__mg$cache0)
				//		{
				//			observable = Observable.Empty<T>();
				//		}
				//		else
				//		{
				//			observable = this.parent.errorHandler(ex);
				//		}
				//	}
				//	catch (Exception error2)
				//	{
				//		try
				//		{
				//			this.observer.OnError(error2);
				//		}
				//		finally
				//		{
				//			base.Dispose();
				//		}
				//		return;
				//	}
				//	SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				//	this.serialDisposable.Disposable = singleAssignmentDisposable;
				//	singleAssignmentDisposable.Disposable = observable.Subscribe(this.observer);
					return;
				//}
				//try
				//{
				//	this.observer.OnError(error);
				//}
				//finally
				//{
				//	base.Dispose();
				//}
			}

			// Token: 0x06000DCE RID: 3534 RVA: 0x000416C4 File Offset: 0x0003FAC4
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040007D1 RID: 2001
			private readonly CatchObservable<T, TException> parent;

			// Token: 0x040007D2 RID: 2002
			private SerialDisposable serialDisposable;

			// Token: 0x040007D3 RID: 2003
			[CompilerGenerated]
			private static Func<TException, IObservable<T>> callback0;
		}
	}
}
